<template>
  <div id="addform">
    <div class="addform-title">
      <h1>新增保险产品</h1>
      <!-- <h1>{{ classsifys }}</h1> -->
    </div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="产品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="保险期限">
        <el-input v-model="form.time"></el-input>
      </el-form-item>
      <el-form-item label="购买份数">
        <el-input v-model="form.number"></el-input>
      </el-form-item>
      <el-form-item label="承保年龄">
        <el-select v-model="form.start" placeholder="选择开始年龄">
          <el-option
            :label="item"
            :value="item"
            v-for="(item, index) in 100"
            :key="index"
          ></el-option>
        </el-select>
        <el-select v-model="form.end" placeholder="选择结束年龄">
          <el-option
            :label="end"
            :value="end"
            v-for="(end, index) in 100"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品数量">
        <el-input v-model="form.totalNumber"></el-input>
      </el-form-item>
      <el-form-item label="产品描述">
        <el-input v-model="form.describe" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="产品单价">
        <el-input v-model="form.money"></el-input>
      </el-form-item>
      <el-form-item label="产品分类">
        <el-select v-model="form.belongToClassifys" placeholder="请选择">
          <el-option
            v-for="item in classsifys"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          >
            <span style="float: left">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="图片上传: ">
        图片上传: &nbsp;&nbsp;&nbsp;<input type="file" id="uploadimages" />
      </el-form-item>  -->

      <el-upload
        class="avatar-uploader"
        action="http://localhost:3000/products/uploadImages"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("productsManages");
const {
  mapState: proudctsClassifys,
  mapActions: getproudctsClassifys,
} = createNamespacedHelpers("classifysList");
export default {
  created() {
    this.getClassifys();
  },
  data() {
    return {
      form: {
        name: "",
        time: "",
        number: "",
        start: "",
        end: "",
        totalNumber: "",
        describe: "",
        money: "",
        delete: false,
        belongToClassifys: "",
        state: 1,
        // images: "",
        img: "",
        // classifysAll: [],
      },
      imageUrl: "",
    };
  },
  computed: {
    ...proudctsClassifys(["classsifys"]),
  },
  methods: {
    ...mapActions(["addProducts"]),
    ...getproudctsClassifys(["getClassifys"]),
    async onSubmit() {
      const data = await this.addProducts({
        ageOfCoverage: [this.form.start, this.form.end],
        name: this.form.name,
        time: this.form.time,
        number: this.form.number,
        totalNumber: this.form.totalNumber,
        describe: this.form.describe,
        delete: this.form.delete,
        money: this.form.money,
        state: this.form.state,
        img: this.form.img,

        belongToClassifys: this.form.belongToClassifys,
      });
      if (data.data.status == 1) {
        this.$message({
          message: "新增成功",
          type: "success",
        });
      } else {
        this.$message.error("新增保险产品数据失败");
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = res.data[0];
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // onSubmit() {
    //   console.log("submit!");
    // },
  },
};
</script>

<style lang ="scss" scoped>
#addform {
  text-align: center;
}
.el-form {
  width: 40%;
  margin: 0 auto;
}
.el-upload {
  float: right;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
.addform-title {
  text-align: left;
}
</style>